#include "dbusobjmodel.hpp"
#include <qdebug.h>
#include "common.hpp"
DbusObjModel::DbusObjModel(QObject *parent) : QAbstractItemModel(parent)
{
    root=new QDBusObject();
} 
void DbusObjModel::refresh(QString context)
{
    beginResetModel ();
    root->setContext (context.toLatin1 ());
    BusSignatureEx data;
    data.mPath = "local methods";
    data.type = LocalMethod;
    QDBusObject *item = new QDBusObject(data,root);
    root->children.append (item);
    endResetModel ();

}

void DbusObjModel::append(QDBusObject *item,const QModelIndex &index)
{
    const int position = root->children.count ();
    foreach (QDBusObject* var, root->children) {
        if(var->m_data.mBus == item->m_data.mBus
                && var->m_data.mService == item->m_data.mService
                && var->m_data.mPath == item->m_data.mPath
                && var->m_data.mInterface == item->m_data.mInterface)
        {
            return;
        }

    }
    beginInsertRows(index, position, position);
    root->children.append (item);
    endInsertRows();
}
void DbusObjModel::remove(int position, const QModelIndex &parent)
{
    if (position >= root->children.count ())
        return;
    if (root->children.at (position)->m_data.type == LocalMethod)
        return;
    beginRemoveRows (parent,position,position);
    root->children.removeAt (position);
    endRemoveRows ();
}
void DbusObjModel::append(const QString &bus, const QString &service, const QString &path, const QString &interface)
{
    BusSignatureEx data;
    data.mBus = bus;
    data.mService = service;
    data.mPath = path;
    data.mInterface = interface;
    QDBusObject *item = new QDBusObject(data,root);
    append (item);
}

BusSignatureEx DbusObjModel::getItemData(const QModelIndex &index)
{
    const QDBusObject *item = static_cast<QDBusObject *>(index.internalPointer());
    if (!item)
        return BusSignatureEx();
    return item->m_data;
}

QByteArray DbusObjModel::toContext()
{
    QByteArray Context = root->toContext ();
    return Context;
}
QModelIndex DbusObjModel::index(int row, int column, const QModelIndex &parent) const
{
    return createIndex(row, column, root->children.at(row));
}

QModelIndex DbusObjModel::parent(const QModelIndex &child) const
{
    QDBusObject *item = static_cast<QDBusObject *>(child.internalPointer());
    if (!item || !item->parent || !item->parent->parent)
        return QModelIndex();

    return createIndex(item->parent->parent->children.indexOf(item->parent), 0, item->parent);
}

int DbusObjModel::rowCount(const QModelIndex &parent) const
{
    return root->children.count ();
}

int DbusObjModel::columnCount(const QModelIndex &parent) const
{
    return 4;
}

QVariant DbusObjModel::data(const QModelIndex &index, int role) const
{
    const QDBusObject *item = static_cast<QDBusObject *>(index.internalPointer());
    if (!item)
        return QVariant();
    if (role != Qt::DisplayRole)
        return QVariant();
    switch (index.column ()) {
    case 0:
        return item->m_data.mPath;
        break;
    case 1:
        return item->m_data.mInterface;//item->bus_Interface;
        break;
    case 2:
        return item->m_data.mService;//item->bus_service;
        break;
    case 3:
        return item->m_data.mBus;//item->bus_name;
        break;
    default:
        break;
    }
    return QVariant();
}


QVariant DbusObjModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }
    if (orientation == Qt::Vertical)
        return QVariant();
    switch (section) {
    case 0:
        return "Path";
        break;
    case 1:
        return "Interface";
        break;
    case 2:
        return "Service";
        break;
    case 3:
        return "Addr";
        break;
    default:
        break;
    }
    return QVariant();
}
